/**
 * @description 用户关系 controller
 * @author chengpeng
 */

const {
    SuccessModel,
    ErrorModel
} = require('../model/ResModel')
const {
    addFollowerFailInfo,
    deleteFollowerFailInfo
} = require('../model/ErrorInfo')
const {
    getUsersByFollower,
    addFollower,
    deleteFollower,
    getFollowersByUser
} = require('../services/user-relation')

/**
 * @description 获取粉丝列表
 * @author chengpeng
 * @param {string} userId 用户id
 */
async function getFans(userId) {
    const {
        count,
        userList
    } = await getUsersByFollower(userId)

    return new SuccessModel({
        count,
        fansList: userList
    })
}

/**
 * @description 获取关注人列表
 * @author chengpeng
 * @param {number} userId  userId
 */
async function getFollowers(userId) {
    const {
        count,
        userList
    } = await getFollowersByUser(userId)
    return new SuccessModel({
        count,
        followersList: userList
    })
}

/**
 * @description 关注
 * @author chengpeng
 * @param {number} myUserId 当前登录用户id
 * @param {number} curUserId 要关注人的id
 */
async function follow(myUserId, curUserId) {
    try {
        await addFollower(myUserId, curUserId)
        return new SuccessModel()
    } catch (error) {
        return new ErrorModel(addFollowerFailInfo)
    }

}

/**
 * @description 取消关注
 * @author chengpeng
 * @param {number} myUserId 当前登录用户id
 * @param {number} curUserId 要关注人的id
 */
async function unFollow(myUserId, curUserId) {
    const result = await deleteFollower(myUserId, curUserId)
    if (result) {
        return new SuccessModel()
    }
    return new ErrorModel(deleteFollowerFailInfo)
}

module.exports = {
    getFans,
    follow,
    unFollow,
    getFollowers
}